package design.pattern.Lu06ProxyPattern;

/**
 * @program JavaBase
 * @description: 强制代理
 * @author: zhanglu
 * @create: 2020-03-29 10:46:00
 */
public class Lu06ProxyPattern02 {

    public static void main(String[] args) {

        IGamePlayer02 iGamePlayer02 = new GamePlayer02("张三");

        iGamePlayer02.login("zhangsan", "111111");

        IGamePlayer02 iGamePlayer03 = new GamePlayerProxy02(iGamePlayer02);

        iGamePlayer03.login("lisi", "111111");

        iGamePlayer02.getInstance().login("zhangsan", "111111");
        iGamePlayer02.getInstance().killBoss();
        iGamePlayer02.getInstance().upgrade();

    }

}

interface IGamePlayer02{

    void login(String username, String password);

    void killBoss();

    void upgrade();

    IGamePlayer02 getInstance();

}

class GamePlayer02 implements IGamePlayer02{

    private GamePlayerProxy02 proxy02;
    private String name;

    public GamePlayer02(String name){
        this.name = name;
    }

    @Override
    public void login(String username, String password) {
        if(isProxy()){
            System.out.println("登陆：" + this.name + " - " + username + " - " + password);
        }else {
            System.out.println("登陆：请使用强制代理");
        }
    }

    @Override
    public void killBoss() {
        if(isProxy()){
            System.out.println("杀怪：" + this.name);
        }else {
            System.out.println("刷怪：请使用强制代理");
        }
    }

    @Override
    public void upgrade() {
        if(isProxy()){
            System.out.println("升级：" + this.name);
        }else {
            System.out.println("升级：请使用强制代理");
        }
    }

    @Override
    public IGamePlayer02 getInstance() {
        return this.proxy02 = new GamePlayerProxy02(this);
    }

    private boolean isProxy(){
        if(this.proxy02 == null){
            return false;
        }
        return true;
    }

}

class GamePlayerProxy02 implements IGamePlayer02{

    private IGamePlayer02 iGamePlayer02;

    public GamePlayerProxy02(IGamePlayer02 iGamePlayer02){
        this.iGamePlayer02 = iGamePlayer02;
    }

    @Override
    public void login(String username, String password) {
        iGamePlayer02.login(username, password);
    }

    @Override
    public void killBoss() {
       iGamePlayer02.killBoss();
    }

    @Override
    public void upgrade() {
        iGamePlayer02.upgrade();
    }

    @Override
    public IGamePlayer02 getInstance() {
        return this;
    }

}

